package gms;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import config.Catalog;
import config.Configure;
import config.DBOption;


/**
 * this class is response for manage the information of the cluster. the cluster
 * consists of a groups of virtual nodes. Each virtual node is an token in the
 * dht and is assigned to some IPs.
 * 
 * @author xiafan
 * 
 */
public class GMSManager {
	private Map<String, TokenMetaData> dbRingMap = new HashMap<String, TokenMetaData>();
	private static GMSManager instance = new GMSManager();
	
	public static GMSManager getInstance() {
		return instance;
	}
	
	private GMSManager() {
	}

	public void Init() {
		DBOption[] dbs = Configure.getDBOptions();
		try {
			for (DBOption db : dbs) {
				TokenMetaData metaData = new TokenMetaData();
				StringTokenPartition partition = new StringTokenPartition();

				for (Entry<String, String> node : db.dhtring.entrySet()) {
					metaData.addNewNode(partition.partition(node.getValue()),
							InetAddress.getByName(node.getKey()));
				}
				dbRingMap.put(db.dbname, metaData);
			}
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * get the ILookupTable for db
	 * 
	 * @param db
	 * @return
	 */
	public ILookupTable getLookupTable(String db) {
		return null;
	}

	public TokenMetaData getTokenMetaData(String db) {
		return dbRingMap.get(db);
	}
	
}
